package cn.itsource.order.service.impl;

import cn.itsource.basic.constant.PayConstants;
import cn.itsource.basic.constant.PetHomeConstant;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.CodeGenerateUtils;
import cn.itsource.order.domain.AdoptOrder;
import cn.itsource.order.domain.OrderAddress;
import cn.itsource.order.domain.PayBill;
import cn.itsource.order.mapper.AdoptOrderMapper;
import cn.itsource.order.mapper.OrderAddressMapper;
import cn.itsource.order.mapper.PayBillMapper;
import cn.itsource.order.service.IAdoptOrderService;
import cn.itsource.order.service.IPayBillService;
import cn.itsource.org.domain.Shop;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.domain.UserAddress;
import cn.itsource.user.mapper.UserAddressMapper;
import cn.itsource.user.mapper.UserMapper;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private IPayBillService payBillService;
    //领养订单
    @Override
    public String submitOrder(Map<String, Object> map, LoginInfo loginInfo) {
        //获取宠物id
        Long petId = Long.valueOf(map.get("petId").toString());
        //获取用户地址id
        Long addressId = Long.valueOf(map.get("addressId").toString());
        //获取支付方式
        Integer payType = (Integer) map.get("payType");
        //通过宠物id获取宠物对象
        Pet pet = petMapper.findById(petId);
        //设置宠物状态为下架
        pet.setState(PetHomeConstant.OFF);
        //设置下架时间
        pet.setOffsaletime(new Date());
        //设置领养人信息
        User user = userMapper.lodaUserById(loginInfo.getId());
        pet.setUser(user);
        //修改pet信息
        petMapper.update(pet);
        //创建领养订单
       AdoptOrder order= createAdoptOrder(pet);
       //保存领养订单
        adoptOrderMapper.save(order);
        //获取user地址信息通过地址id
        UserAddress userAddress = userAddressMapper.findById(addressId);
        //创建订单地址
        OrderAddress orderAddress = createOrderAddress(userAddress, order.getOrderSn());
        //保存订单地址
        orderAddressMapper.save(orderAddress);
        //创建支付单
        PayBill payBill=createPayBill(order,payType);
        //保存支付单
        payBillMapper.save(payBill);
        //用于接收支付宝返回的html代码
        String result="";
        //判断支付类型
        switch (payType){
            case 1:
                //银联支付
                break;
            case 2:
                //微信支付
                break;
            case 3:
                //跳转到支付宝支付
                result = payBillService.goPay(payBill);
                break;
        }
        return result;
    }
    //创建支付单
    private PayBill createPayBill(AdoptOrder order, Integer payType) {
        //创建领养订单
        PayBill payBill = new PayBill();
        //摘要
        payBill.setDigest(order.getDigest());
        //金额
        payBill.setMoney(order.getPrice());
        //设置状态
        payBill.setState(PetHomeConstant.UNPAID);
        //最后支付时间
        payBill.setLastPayTime(order.getLastPayTime());
        //设置支付方式0 余额 1 支付宝 2 微信 3 银联
        payBill.setPayChannel(payType);

        //业务类型(定位操作具体哪张表)
        payBill.setBusinessType(PayConstants.BUSINESSTYPE_ADOPT);
        //业务键(定位操作哪条数据)
        payBill.setBusinessKey(order.getId());
        //用户
        payBill.setUser(order.getUser());
        payBill.setShop(order.getShop());
        //用户昵称
        payBill.setNickName(order.getUser().getUsername());
        //店铺名
        payBill.setShopName(order.getShop().getName());
        //订单编号
        payBill.setOrderSn(order.getOrderSn());
       return payBill;
    }


    //创建订单地址
    private OrderAddress createOrderAddress(UserAddress userAddress, String orderSn) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress, orderAddress);
        //设置订单编号
        orderAddress.setOrderSn(orderSn);
        //设置订单创建时间
        orderAddress.setCreateTime(new Date());

        return orderAddress;
    }

    //创建领养订单
    private AdoptOrder createAdoptOrder(Pet pet) {
        AdoptOrder order = new AdoptOrder();
        //设置摘要
        order.setDigest("[摘要]对"+pet.getName()+"宠物进行领养");
        //设置价格
        order.setPrice(pet.getSaleprice());
        //设置订单编号
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(pet.getUser().getId()));
        //设置最后支付时间
        order.setLastPayTime(DateUtils.addMinutes(new Date(), 15));
        //设置宠物
        order.setPet(pet);
       //设置用户
        order.setUser(pet.getUser());
       //设置店铺
        order.setShop(pet.getShop());
        return order;
    }
}
